let requestUrl;
let width = 45;

function addHandler() {
	let type = $("#_type_").combobox("getValue");

	if (type) {
		$("#type").combobox("setValue", type);
	}

	requestUrl = "/partner/insert";

	$("#partner_dialog").dialog("open");
}

function editHandler() {
	let rowData = $("#partner_list").datagrid("getSelected");

	if (rowData) {
		requestUrl = "/partner/updateById";

		$("#id").val(rowData.id);
		$("#name").textbox("setValue", rowData.name);
		$("#note").textbox("setValue", rowData.note);
		$("#type").combobox("setValue", rowData.type);

		$("#partner_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录", "warning");
	}
}

function imageHandler() {
	let rowData = $("#partner_list").datagrid("getSelected");

	if (rowData) {
		$("#partner_id").val(rowData.id);
		$("#img").attr("src", rowData.image);

		$("#upload_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录", "warning");
	}
}

function viewHandler() {
	let rowData = $("#partner_list").datagrid("getSelected");

	if (rowData) {
		$("#partner_name").html(rowData.name);
		$("#partner_grade").html(69);
		$("#partner_image").attr("src", rowData.image);

		ajaxGet("/partner_type/selectById", {
			id: rowData.type
		}, function (resp) {
			let data = resp.data;

			$("#partner_type").html("<i class='" + data.iconCls + "'></i>" + data.name);
		}, error);

		$('#partner_note').html(rowData.note);

		// 助战属性
		$("#qixue").progressbar("setValue", 4896);
		$("#mofa").progressbar("setValue", 1560);
		$("#whulishanghai").progressbar("setValue", 1231);
		$("#wulifangyu").progressbar("setValue", 515);
		$("#fashushanghai").progressbar("setValue", 0);
		$("#fashufangyu").progressbar("setValue", 471);
		$("#sudu").progressbar("setValue", 491);
		$("#dikangfengying").progressbar("setValue", 1462);

		loadSkill();

		$("#partner_detail").dialog("open");
	} else {
		alertMsg("请选择一条记录", "warning");
	}
}

function deleteHandler() {
	let rowData = $("#partner_list").datagrid("getSelected");

	if (rowData) {
		$.messager.confirm("提示", "是否确认删除？", function(bool) {
			if (bool) {
				ajaxPost("/partner/deleteById", {
					id: rowData.id
				}, function(resp) {
					showMsg(resp.message);

					$("#partner_list").datagrid("reload");
				}, error);
			}
		});
	} else {
		alertMsg("请选择要修改的记录", "warning");
	}
}

/**
 * 加载助战技能
 */
function loadSkill() {
	// 助战技能
	let base = "";

	base += "<tr>";
	base +=		"<td>";
	base +=			"<div class='partner_skill'>";
	base +=	    		"<img src='/partner_skill/20230217232907.png' />";
	base +=			"</div>";
	base +=	    "</td>";

	base +=		"<td>";
	base +=			"<div class='partner_skill'>";
	base +=	    		"<img src='/partner_skill/20230217232915.png' />";
	base +=			"</div>";
	base +=	    "</td>";

	base +=		"<td>";
	base +=			"<div class='partner_skill'>";
	base +=	    		"<img src='/partner_skill/20230217232921.png' />";
	base +=			"</div>";
	base +=	    "</td>";
	base +=	"</tr>";

	$("#base").empty().append(base);

	let special = "";

	special += "<tr>";
	special +=		"<td>";
	special +=			"<div class='partner_skill'>";
	special +=	    		"<img src='/partner_skill/20230217232928.png' />";
	special +=			"</div>";
	special +=	    "</td>";

	special +=		"<td>";
	special +=			"<div class='partner_skill'>";
	special +=	    		"<img src='/partner_skill/20230217232933.png' />";
	special +=			"</div>";
	special +=	    "</td>";

	special +=		"<td>";
	special +=			"<div class='partner_skill'>";
	special +=	    		"<img src='/partner_skill/20230217232939.png' />";
	special +=			"</div>";
	special +=	    "</td>";

	special +=		"<td>";
	special +=			"<div class='partner_skill'>";
	special +=	    		"<img src='/partner_skill/20230217232947.png' />";
	special +=			"</div>";
	special +=	    "</td>";
	special +=	"</tr>";

	$("#special").empty().append(special);
}

$(document).ready(function() {

	$("#_type_").combobox({
		url: "/partner_type/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		panelHeight: "auto",
		prompt: "-助战类型-",
		formatter: function(row) {
			return "<i class='" + row.iconCls + "' ></i>&nbsp;<span style='font-size:28px;'>" + row.name + "</span>";
		}
	});

	$("#_name_").textbox({
		width: 150,
		prompt: "输入助战名字模糊查询"
	});

	$("#search").linkbutton({
		iconCls: "icon-search"
	}).click(function() {
		$("#partner_list").datagrid("reload", {
			type: $("#_type_").combobox("getValue"),
			name: $("#_name_").textbox("getValue")
		});
	});

	$("#clear").linkbutton({
		iconCls: "icon-delete"
	}).click(function() {
		$("#search_form").form("clear");
	});

	/******************************************************************************************************/

	$("#type").combobox({
		url: "/partner_type/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		required:true,
		panelHeight: "auto",
		formatter: function(row) {
			return "<i class='" + row.iconCls + "'></i>&nbsp;" + row.name;
		}
	});

	$("#name").textbox({
		width: 150,
		required: true
	});

	$("#note").textbox({
		width: 150,
		required: true
	});
	
	$("#partner_dialog").dialog({
		title: "助战信息",
		width: 500,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#partner_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(resp) {
						showMsg(resp.message);

						$(selector).form("clear");
						$("#partner_dialog").dialog("close");
						$("#partner_list").datagrid("reload");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#partner_dialog").dialog("close");
				$("#partner_form").form("clear");
			}
		}]
	});

	/******************************************************************************************************/

	fetchUploadFilebox("#partner_list", "/partner/upload");

	fetchUploadDialog("/partner/updateImageById", function () {
		$("#partner_list").datagrid("reload");
	});

	/******************************************************************************************************/

	/**
	 * partner_detail
	 */
	$("#qixue").progressbar({
		width: 80,
		text: "{value}"
	});

	$("#mofa").progressbar({
		width: 80,
		text: "{value}"
	});

	$("#whulishanghai").progressbar({
		width: 80,
		text: "{value}"
	});

	$("#wulifangyu").progressbar({
		width: 80,
		text: "{value}"
	});

	$("#fashushanghai").progressbar({
		width: 80,
		text: "{value}"
	});

	$("#fashufangyu").progressbar({
		width: 80,
		text: "{value}"
	});

	$("#sudu").progressbar({
		width: 80,
		text: "{value}"
	});

	$("#dikangfengying").progressbar({
		width: 80,
		text: "{value}"
	});

	$("#operation").linkbutton({
		text: "&emsp;&emsp;上&ensp;阵&emsp;&emsp;"
	}).click(function() {
		unopen();
	});

	$("#partner_detail").dialog({
		title: "助战详情"
	});

	/******************************************************************************************************/

	/**
	 * 表格体的高度
	 * @type {number}
	 */
	let heigt_body = heigt_tr_with_image * 10;
	/**
	 * 表格高度
	 * @type {number}
	 */
	let height_table = heigt_tool + heigt_header + heigt_body + heigt_footer + heigt_scoll_bar + 2;

	$("#partner_list").datagrid({
		url: "/partner/selectByPage",
		method: "get",
		height: height_table,
		rownumbers: true,
		fitColumns: true,
		pagination: true,
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler();
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}, "-", {
			iconCls: "icon-search",
			text: "查看",
			handler: function() {
				viewHandler();
			}
		}, "-", {
			iconCls: "icon-delete",
			text: "删除",
			handler: function() {
				deleteHandler();
			}
		}, "-", imageUpdateButton],
		columns: [[
			{field: "name", title: "助战名字", align: "center", width: 30},
			{field: "image", title: "图片", align: "center", width: 30
				, formatter: function(value) {
					if (value) {
						return "<img height='" + size + "' src='" + value + "' />";
					}

					return "/";
				}
			},
			{field: "type", title: "类型", align: "center", width: 30
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/partner_type/selectById", {
						id: value
					},function(resp) {
						let data = resp.data;

						$("#type_" + rowIndex).addClass(data.iconClass)
							.attr("title", data.name);
					}, error);

					return "<i id='type_" + rowIndex + "'></i>";
				}
			},
			{field: "note", title: "介绍", align: "center", width: 200
				, formatter: function(value) {
					return "<div class='ell' title='" + value + "'>" + value + "</div>";
				}
			}
		]]
	});

});